package yxy.game.pm2.command.equip;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BlacksmithMessage;
import yxy.game.pm2.module.BlacksmithHandle;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/** 工坊 装备一键合成 */
@MessageType(proto = Command.EquipementFusionOneButton, classtype = BlacksmithMessage.EquipementFusionOneButtonRequest.class)
public class CMD_EquipementFusionOneButton extends Command<BlacksmithMessage.EquipementFusionOneButtonRequest> {

	@Override
	public void execute(MessageSource messageSource, BlacksmithMessage.EquipementFusionOneButtonRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();// 装备类型

		BlacksmithMessage.EquipementFusionOneButtonResponse.Builder response = BlacksmithMessage.EquipementFusionOneButtonResponse.newBuilder();

		BlacksmithHandle blacksmithModule = new BlacksmithHandle();
		Currency cost = Currency.create(CurrencyType.CT_钱币, 0, 0);
		try {
			Collection<Currency> target = blacksmithModule.fusionAll(playerCode, type, cost);
			ArrayList<Currency> get = new ArrayList<>();
			for (Currency c : target) {
				if (c.getNumber() > 0) {
					get.add(c);
				}
			}

			response.addAllTarget(new CurrencyHandle().packItems(get));
			response.setCost(new CurrencyHandle().packPoint(cost));

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);// 合成失败
		}

	}

	private void send(MessageSource messageSource, BlacksmithMessage.EquipementFusionOneButtonResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}